package com.atguigu.common.result;


import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

/**
 * 全局统一返回结果类
 */
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Result<T> {

    //返回码
    private Integer code;

    //返回消息
    private String message;

    //返回数据
    private T data;


    private static <T> Result<T> build(T data) {
        Result<T> result = new Result<>();
        if (data != null)
            result.setData(data);
        return result;
    }

    public static <T> Result<T> build(ResultCodeEnum resultCodeEnum,T body) {
        Result<T> result = build(body);
        result.setCode(resultCodeEnum.getCode());
        result.setMessage(resultCodeEnum.getMessage());
        return result;
    }


    public static <T> Result<T> ok(T data) {
        return build(ResultCodeEnum.SUCCESS,data);
    }

    public static <T> Result<T> ok() {
        return new Result<T>(200,"成功",null);
    }

    public static <T> Result<T> ok(Integer code,String message) {
        return new Result<T>(code,message,null);
    }

    private static <T> Result<T> ok(Integer code, String message, T data) {
        return new Result<T>(code,message,data);
    }

    public static <T> Result<T> fail() {
        return build(ResultCodeEnum.FAIL,null);
    }

    public static <T> Result<T> fail(T data) {
        return build(ResultCodeEnum.FAIL,data);
    }


    /**
     * 错误返回方法
     * @return Result
     */
    public static <T> Result<T> error(Integer code,String message,T data) {
        return new Result<T>(code,message,data);
    }
    public static <T> Result<T> error(Integer code,String message) {
        return new Result<T>(code,message,null);
    }



    /**
     * 验证码错误
     * @return build
     */
    public static <T> Result<T> error() {
        return build(ResultCodeEnum.APP_LOGIN_CODE_ERROR,null);
    }



}
